package com.phone580.app.download;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.phone580.app.BaseActivity;
import com.phone580.app.R;

public class UnFinishDownloadActivity extends BaseActivity{
	private ProgressBar pb;
	private ListView lvUnfinish;
	private UnFinishDownloadAdapter adapter;
	private boolean flag = true;
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			int what = msg.what;
			switch (what) {
			case 0:
				adapter.refresh();
				adapter.notifyDataSetChanged();
				break;
			case 1:
				pb.setVisibility(View.VISIBLE);
//				System.out.println("pb show");
				break;
			case 2:
				pb.setVisibility(View.GONE);
//				System.out.println("pb gone");
			}
			
		};
	};
	private WorkerThread worker;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.unfinish_download_layout);
		adapter = new UnFinishDownloadAdapter(this);
		adapter.setHandler(handler);
		lvUnfinish = (ListView) findViewById(R.id.unfinish_download_list_lv);
		pb = (ProgressBar) findViewById(R.id.unfinish_download_list_pb);
		pb.setVisibility(View.GONE);
		lvUnfinish.setAdapter(adapter);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		worker = new WorkerThread();
		flag = true;
		worker.start();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		flag = false;
	}
	
	class WorkerThread extends Thread {
		@Override
		public void run() {
			while (flag) {
				handler.sendEmptyMessage(0);
				try {
					sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	@Override
	public void onBackPressed() {
		pressAgainExit();
	}
}
